package com.zl.roomdesign.auth

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import com.zl.core.base.ModeActivity
import com.zl.core.extend.toTextString
import com.zl.roomdesign.R
import com.zl.roomdesign.main.MainActivity
import kotlinx.android.synthetic.main.activity_login.*

/**
 * Created by zhangli on 2018/5/5,19:02<br/>
 */
class LoginActivity : ModeActivity() {

    private lateinit var viewModel: LoginViewModel

    override fun layoutId() = R.layout.activity_login

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun setListener() {
        loginButton.setOnClickListener {
            viewModel.login(accountEdit.toTextString(), passwordEdit.toTextString())
        }

        registerText.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }
    }

    override fun observe() {
        viewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)

        viewModel.isLoadingInner.observe(this, Observer {
            if (it != null && it) { //正在加载,不显示弹框
                loadingInner()
            } else {
                cancelLoadingInner()
            }
        })

        viewModel.isLoading.observe(this, Observer {
            if (it != null && it) { //正在加载
                showLoading()
            } else {
                hideLoading()
            }
        })

        viewModel.errorMsg.observe(this, Observer {
            if (it != null) {
                showToastSafe("${it.code}-${it.msg}")
            }
        })

        viewModel.loginSuccess.observe(this, Observer {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        })

    }

    override fun afterView() {
    }
}